
#include <iostream>
#include <unistd.h>

#include "thrdpool.h"

void routine(void *data)
{
    int *p = (int*)data;
    std::cout << "task start " << *p << std::endl;

    std::cout << "task finish " << *p << std::endl;
}

int main(int argc, char **argv)
{
    std::cout << "thread pool test starting" << std::endl;

    thrdpool_t *pool = thrdpool_create(2, 0);

    struct thrdpool_task *t1 = (struct thrdpool_task*)malloc(sizeof(struct thrdpool_task));
    t1->routine = routine;
    int *p1 = (int*)malloc(sizeof(int));
    *p1 = 1;
    t1->context = (void*)p1;

    struct thrdpool_task *t2 = (struct thrdpool_task*)malloc(sizeof(struct thrdpool_task));
    t2->routine = routine;
    int *p2 = (int*)malloc(sizeof(int));
    *p2 = 2;
    t2->context = (void*)p2;

    thrdpool_schedule(t1, pool);
    thrdpool_schedule(t2, pool);

    sleep(3);

    free(t2->context);
    free(t2);
    free(t1->context);
    free(t1);
    thrdpool_destroy(NULL, pool);

    std::cout << "thread pool test finish" << std::endl;
	return 0;
}